package com.tanhua.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.QuestionMapper;
import com.tanhua.common.pojo.Question;
import com.tanhua.server.service.QuestionService;
import com.tanhua.server.service.UserInfoService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 查询问题
     *
     * @param userId
     * @return
     */
    @Override
    public Question queryQuestion(Long userId) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Question question = this.questionMapper.selectOne(queryWrapper);
        //Question question = this.questionMapper.selectById(userId);

        return question;
    }

    /**
     * 保存问题
     *
     * @param id
     * @param content
     */
    @Override
    public void save(Long id, String content) {
        Question question = this.queryQuestion(id);

        if (Optional.ofNullable(question).isPresent()){
            question.setTxt(content);
            this.questionMapper.updateById(question);
        }else {
            question = new Question();
            question.setUserId(id);
            question.setTxt(content);
            question.setCreated(new Date());
            question.setUpdated(question.getCreated());
            this.questionMapper.insert(question);
        }


    }
}
